package com.mictapp.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.mictapp.fragment.FindfrdFragment;
import com.mictapp.fragment.ListPatientsFragment;
import com.mictapp.fragment.MeFragment;
import com.mictapp.fragment.MonitorRecordFragment;

public class MainPageActivity extends FragmentActivity implements OnClickListener {

	private FragmentManager fragmentManager = getSupportFragmentManager();
	private ImageView mTab1, mTab2, mTab3, mTab4;
	//current fragment
	private Fragment mContent;
	
	private Fragment listPatientsFragment;
	private Fragment monitorFragment;
	private Fragment findfrgFragment;
	private Fragment meFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mTab1 = (ImageView) findViewById(R.id.img_signed);
		mTab2 = (ImageView) findViewById(R.id.img_monitor);
		mTab3 = (ImageView) findViewById(R.id.img_friends);
		mTab4 = (ImageView) findViewById(R.id.img_settings);
		
		mTab1.setOnClickListener(this);
		mTab2.setOnClickListener(this);
		mTab3.setOnClickListener(this);
		mTab4.setOnClickListener(this);

		FragmentTransaction transaction = fragmentManager.beginTransaction();
		mContent = listPatientsFragment =  new ListPatientsFragment();
		monitorFragment = new MonitorRecordFragment();
		findfrgFragment = new FindfrdFragment();
		meFragment = new MeFragment();
		
		transaction.replace(R.id.content, mContent);
		transaction.commit();
	}


	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
			case R.id.img_signed:
				switchContent(listPatientsFragment);
				mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_pressed));
				mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
				mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
				mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
				break;
			case R.id.img_monitor:
				switchContent(monitorFragment);
				mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
				mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_pressed));
				mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
				mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
				break;
			case R.id.img_friends:
				switchContent(findfrgFragment);
				mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
				mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
				mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_pressed));
				mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
				break;
			case R.id.img_settings:
				switchContent(meFragment);
				mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
				mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
				mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
				mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_pressed));
				break;
			default:
				break;
		}
	};
	
	/**
	 * hide fragment to avoid load again
	 * @param to
	 */
    public void switchContent(Fragment to) {
            if (mContent != to) {
                    FragmentTransaction transaction = getSupportFragmentManager()
                                    .beginTransaction();
                    if (!to.isAdded()) { 
                            transaction.hide(mContent).add(R.id.content, to).commit(); 
                    } else {
                            transaction.hide(mContent).show(to).commit(); 
                    }
                    mContent = to;
            }
    }

}
